package com.example.jacky.uilibrary.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.jacky.uilibrary.utils.MyScroller;

public class MyScrollView extends ViewGroup {
	private Context ctx;
	public MyScrollView(Context context) {
		super(context);
		this.ctx = context;
		initView();
	}

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.ctx = context;
		initView();
	}

	private void initView() {
		scroller = new MyScroller(ctx);
		detector = new GestureDetector(ctx, new OnGestureListener() {
			
			@Override
			public boolean onSingleTapUp(MotionEvent e) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public void onShowPress(MotionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
									float distanceY) {
				/**
				 * 移动当前view内容 移动一段距离
				 * disX	 X方向移的距离		为正是，图片向左移动，为负时，图片向右移动 
				 * disY  Y方向移动的距离
				 */
				scrollBy((int) distanceX, 0);
				
				/**
				 * 将当前视图的基准点移动到某个点  坐标点
				 * x 水平方向X坐标
				 * Y 竖直方向Y坐标
				 *  scrollTo(x,  y);
				 */
				return false;
			}
			
			@Override
			public void onLongPress(MotionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
								   float velocityY) {
				isFling = true;
				if(velocityX > 0 && currId >0) {
					currId--;
				}else if(velocityX < 0 && currId < getChildCount()-1) {
					currId++;
				}
				moveToDest(currId);
				return false;
			}
			
			@Override
			public boolean onDown(MotionEvent e) {
				// TODO Auto-generated method stub
				return false;
			}
		});
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		for (int i = 0; i <getChildCount(); i++) {
			View view = getChildAt(i);// 取得下标为I的子view
			view.measure(widthMeasureSpec, heightMeasureSpec);
		}
	}
	
	/**
	 * 对子view进行布局，确定子view的位置
	 * changed  若为true ，说明布局发生了变化
	 * l\t\r\b\  是指当前viewgroup 在其父view中的位置 
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		for (int i = 0; i <getChildCount(); i++) {
			View view = getChildAt(i);// 取得下标为I的子view

			/**
			 * 父view 会根据子view的需求，和自身的情况，来综合确定子view的位置,(确定他的大小)
			 */
			//指定子view的位置  ,  左，上，右，下，是指在viewGround坐标系中的位置
			view.layout(i*getWidth(), 0, (i+1)*getWidth(), getHeight());
		}

	}
	
	private int currId = 0;
	private int firstX = 0;
	private GestureDetector detector;
	private boolean isFling = false;
	private MyScroller scroller;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		detector.onTouchEvent(event);
		//自己的Touchevent事件
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			firstX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			
			break;

		case MotionEvent.ACTION_UP:
			if(!isFling) {//  在没有发生快速滑动的时候，才执行按位置判断currid
				int nextId = 0;
				if(event.getX() - firstX > getWidth()/2) {//手指向右滑动，超过屏幕的1/2  当前的currid - 1
					nextId = currId -1;
				}else if(firstX - event.getX() > getWidth()/2) {
					nextId = currId + 1;
				}else{
					nextId = currId;
				}
				moveToDest(nextId);
			}
			isFling = false;
//			int nextId = 0;
//			if(event.getX() - firstX > getWidth()/2) {//手指向右滑动，超过屏幕的1/2  当前的currid - 1
//				nextId = currId -1;
//			}else if(firstX - event.getX() > getWidth()/2) {
//				nextId = currId + 1;
//			}else{
//				nextId = currId;
//			}
//			moveToDest(nextId);
			break;
		}
		
		return true;
	}

	public void moveToDest(int nextId) {
		/*
		 * 对 nextId 进行判断 ，确保 是在合理的范围  
		 * 即  nextId >=0  && next <=getChildCount()-1
		 */
		
		//确保 currId>=0
		currId = (nextId>=0)?nextId:0;
		currId = (nextId<=getChildCount()-1)?nextId:(getChildCount() - 1);
		
//		scrollTo(currId*getWidth(), 0);
		if(pageChangedListener != null) {
			pageChangedListener.moveToDest(currId);
		}
		int distance = currId*getWidth() - getScrollX();
		//系统的scroller的startScroll
		//scroller.startScroll(getScrollX(),0,distance,0,Math.abs(distance));
		scroller.startScroll(getScrollX(),0,distance,0);
		/**
		 * invalidate();  会导致  computeScroll（）这个方法的执行
		 */
		invalidate();
		
	}
	@Override
	public void computeScroll() {
		super.computeScroll();
		if(scroller.computeScrollOffset()) {
			int newX = (int) scroller.getCurrX();
			System.out.println("newX::"+newX);
			
			scrollTo(newX, 0);
			invalidate();
		}
	}
	/**
	 * 提供页面切换的接口
	 */
	private MyPageChangedListener pageChangedListener;
	public interface MyPageChangedListener{
		void moveToDest(int currId);
	}
	public MyPageChangedListener getPageChangedListener() {
		return pageChangedListener;
	}

	public void setPageChangedListener(MyPageChangedListener pageChangedListener) {
		this.pageChangedListener = pageChangedListener;
	}
	
}
